<?php
class Achievement_model extends MY_Model {
	
    function Achievement_model(){
        parent::MY_Model();
        $this->load->database();
        $this->table_name = "prestasi_pelanggaran";
    }
    
    function select_last_entry($search_key=false, $where=false, $limit=10, $offset=0, $orderby=""){
    	$sql = $sqlo = $sqlw = "";
    	
    	if ($search_key){
    		$search_key = is_array($search_key)? $search_key : array($search_key);
    		$oo = "";
    		foreach($search_key as $key){
    			foreach(array('achiev_id','achiev_jenis','achiev_bidang') as $fields){
    				$oo .= 	($oo? " OR ":""). "$fields LIKE '%$key%'";
    			}
    		}
    		
    		$sqlw .= ($sqlw? " AND ($oo)" : " ($oo) ");
    	}
    	
    	if ($where){
	    	if ($where) { 
	 			if (is_array($where)){
	 				$oo = "";
	 				foreach(array_keys($where) as $rr){
	 					$oo .= ($oo? " AND " : ""). "$rr ".$where[$rr];
	 				}
	 				$where .= "$oo ";
	 			}else{
	 				$where .= " AND $where";
	 			}
	 		}
    	}
    	$where = $sqlw||$where? " WHERE $sqlw $where " : "";
    	
    	if ($orderby){ 
 			if (is_array($orderby)){
 				$oo = "";
 				foreach(array_keys($orderby) as $rr){
 					$oo .= ($oo? "," : ""). "$rr ".$orderby[$rr];
 				}
 				$orderby = $oo;
 			}else{
 				$orderby = $orderby;
 			}
 		}
 		$orderby = $orderby? "ORDER BY $orderby" : "";
 		
    	return $query = $this->db->query(
    		"SELECT * 
    		FROM ".$this->table_name." 
  			$where 
    		$orderby 
    		LIMIT $offset,$limit");
    }
        
    function select_by_id($id, $orderby=false){
    	return $query = $this->db->get_where($this->table_name, array('achiev_id' => $id));
    }
    
    function select_by_jenis($jenis, $orderby=false){
    	if ($orderby){
    		foreach($orderby as $field=>$value){
    			$this->db->order_by($field, $value);
    		}
    	}
    	return $query = $this->db->get_where($this->table_name, array('achiev_jenis' => $jenis));
    }
}